﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Comm.Util
{
    public class SchemeUtils
    {

        public const string WEB_SERVICE_PASSWORD = "YouDoGuess";


        /// <summary>
        /// 方案存放目录名
        /// </summary>
        private const string CONST_SCHEMES_DIR = "Schemes";

        /// <summary>
        /// 方案文件扩展名
        /// </summary>
        private const string CONST_SCHEME_EXT = ".scheme";

        /// <summary>
        /// 获取方案存放完整路径
        /// </summary>
        public static string SchemeDirect
        {
            get
            {
                string dir = string.Format(@"{0}\{1}", System.Environment.CurrentDirectory, CONST_SCHEMES_DIR);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                return CONST_SCHEMES_DIR;
            }
        }

        /// <summary>
        /// 方案文件扩展名
        /// </summary>
        public static string SchemeExtensionName
        {
            get
            {
                return CONST_SCHEME_EXT;
            }
        }

        /// <summary>
        /// 获取文件保存目录的绝对路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFullPath(string fileName)
        {
            string dir = string.Format(@"{0}\{1}", System.Environment.CurrentDirectory, CONST_SCHEMES_DIR);

            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }

            return string.Format(@"{0}\{1}{2}", dir, fileName, CONST_SCHEME_EXT);

        }

        /// <summary>
        /// 保存一个方案文件的数据
        /// </summary>
        /// <param name="makr"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool SaveScheme(string mark, string name, byte[] content)
        {
            try
            {
                byte[] markBytes = System.Text.Encoding.UTF8.GetBytes(mark);
                //加载标识数据
                markBytes = Comm.Crypt.SchemeCrypt.Instance.Encrypt(markBytes);
                //获取要写入的文件路径 
                string filePath = GetFullPath(name);

                FileStream fs = new FileStream(filePath, FileMode.Create);

                byte[] markLen = BitConverter.GetBytes(markBytes.Length);
                fs.Write(markLen, 0, markLen.Length); //写入 标识的长度

                //写入标识
                fs.Write(markBytes, 0, markBytes.Length);

                //写入数据
                fs.Write(content, 0, content.Length);
                fs.Close();

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mark"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool LoadSchemeBytes(out string mark, out byte[] content, string schemeName)
        {
            mark = "";
            content = null;

            try
            {
                string filePath = GetFullPath(schemeName);
                if (File.Exists(filePath) == false)
                    return false;
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    //标识长度,固定4个字节
                    byte[] markLen = new byte[4];
                    if (fs.Read(markLen, 0, markLen.Length) != markLen.Length)
                    {
                        return false;
                    }
                    //读取标识,由iMarkLen指定
                    int iMarkLen = BitConverter.ToInt32(markLen, 0);
                    byte[] markContent = new byte[iMarkLen];
                    if (fs.Read(markContent, 0, markContent.Length) != markContent.Length)
                        return false;
                    byte[] markContentPlain = Comm.Crypt.SchemeCrypt.Instance.Decrypt(markContent);
                    mark = System.Text.Encoding.UTF8.GetString(markContentPlain);

                    //读取数据
                    content = new byte[fs.Length - fs.Position];
                    if (fs.Read(content, 0, content.Length) != content.Length)
                        return false;
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
